package com.optitronics.beans;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;

@Configurable
@Entity
@Table(name = "system_properties")
public class SystemProperties {

	@PersistenceContext
	transient EntityManager entityManager;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "PROPERTY_KEY")
	private String propertyKey;

	@Column(name = "PROPERTY_NAME")
	@NotNull
	private String propertyName;

	@Column(name = "PROPERTY_VALUE")
	@NotNull
	private String propertyValue;

	@Column(name = "DESCRIPTION")
	private String description;

	public String getPropertyName() {
		return this.propertyName;
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("PropertyName: ").append(getPropertyName()).append(", ");
		sb.append("PropertyValue: ").append(getPropertyValue()).append(", ");
		sb.append("Description: ").append(getDescription());
		return sb.toString();
	}

	public String getPropertyKey() {
		return this.propertyKey;
	}

	public void setPropertyKey(String id) {
		this.propertyKey = id;
	}

	@Transactional
	public void flush() {
		if (this.entityManager == null)
			this.entityManager = entityManager();
		this.entityManager.flush();
	}

	@Transactional
	public void clear() {
		if (this.entityManager == null)
			this.entityManager = entityManager();
		this.entityManager.clear();
	}

	@Transactional
	public SystemProperties merge() {
		if (this.entityManager == null)
			this.entityManager = entityManager();
		SystemProperties merged = this.entityManager.merge(this);
		this.entityManager.flush();
		return merged;
	}

	public static final EntityManager entityManager() {
		EntityManager em = new SystemProperties().entityManager;
		if (em == null)
			throw new IllegalStateException(
					"Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
		return em;
	}

	public static long countSystemPropertieses() {
		return entityManager().createQuery("select count(o) from SystemProperties o", Long.class).getSingleResult();
	}

	public static List<SystemProperties> findAllSystemPropertieses() {
		return entityManager().createQuery("select o from SystemProperties o", SystemProperties.class).getResultList();
	}

	public static Map<String, String> getSystemPropertiesAsMap() {
		List<SystemProperties> systemProperties = findAllSystemPropertieses();
		Map<String, String> map = new HashMap<String, String>();
		for (SystemProperties property : systemProperties) {
			map.put(property.getPropertyKey(), property.getPropertyValue());
		}

		return map;
	}

	public static SystemProperties findSystemProperties(String id) {
		if (id == null || 0 == id.length())
			return null;
		return entityManager().find(SystemProperties.class, id);
	}

	public static List<SystemProperties> findSystemPropertiesEntries(int firstResult, int maxResults) {
		return entityManager().createQuery("select o from SystemProperties o", SystemProperties.class)
				.setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
	}

	public void setPropertyName(String propertyName) {
		this.propertyName = propertyName;
	}

	public String getPropertyValue() {
		return this.propertyValue;
	}

	public void setPropertyValue(String propertyValue) {
		this.propertyValue = propertyValue;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
}
